﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DiscoveryLogic.Analytics.DataObjects;
using DiscoveryLogic.Analytics.DataObjects.Measures;
using DiscoveryLogic.Analytics.DataObjects.Visuals;
using DiscoveryLogic.Analytics.UI.Common.Events;
using DiscoveryLogic.Common.Utilities;

namespace DiscoveryLogic.Analytics.Modules.Nodes
{
    /// <summary>
    /// Interaction logic for DocumentNode.xaml
    /// </summary>
    public partial class DocumentNode : EntityControlBase
    {
        public event OnExploreSelectedEntity ExploreSelectedEntity;
        public event OnDefineNewRelationRuleFromNetworkEntity DefineNewRelation;

        private EntityBase _Entity;
        private VerticeMeasure _Measure;
        private VerticeMeasureRange _Range;
        private VerticeVisual _Visual;
        private bool _Selected;

        public DocumentNode(EntityBase entity,
            VerticeVisual vVisual, bool selected,
            VerticeMeasure vMeasure, VerticeMeasureRange range)
        {
            InitializeComponent();
            this._Entity = entity;
            this._Measure = vMeasure;
            this._Range = range;
            this._Visual = vVisual;
            this._Selected = selected;
            this.DataContext = entity;
            if (this.ShowLabel(entity, vVisual, vMeasure))
            {
                this.label.Visibility = Visibility.Visible;
                this.label.Foreground = new SolidColorBrush(vVisual.LabelColor);
            }
            else
                this.label.Visibility = Visibility.Collapsed;
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (_Visual.Shape == VerticeShape.Custom)
            {
                this.img.Visibility = Visibility.Visible;
                this.img.Source = NodeHelper.CreateImage(_Visual, _Measure, _Range, _Selected);
                Size size = NodeHelper.GetScaledSize(_Visual, _Measure, _Range);
                this.img.SetValue(Image.WidthProperty, size.Width);
                this.img.SetValue(Image.HeightProperty, size.Height);
            }
            else
            {
                this.img.Visibility = Visibility.Collapsed;
                this.Container.Children.Add(NodeHelper.CreateShape(_Visual, _Measure, this._Entity.Flag, _Range, _Selected));
            }
        }

        private void cmu_ExplorePersons_Click(object sender, RoutedEventArgs e)
        {
            ExploreEntityEventArgs argd = new ExploreEntityEventArgs(this._Entity, EntityType.Persons);
            if (this.ExploreSelectedEntity != null)
                this.ExploreSelectedEntity(argd);
        }

        private void cmu_ExplorePublications_Click(object sender, RoutedEventArgs e)
        {
            ExploreEntityEventArgs argd = new ExploreEntityEventArgs(this._Entity, EntityType.Publications);
            if (this.ExploreSelectedEntity != null)
                this.ExploreSelectedEntity(argd);
        }

        private void cmu_ExploreGrants_Click(object sender, RoutedEventArgs e)
        {
            ExploreEntityEventArgs argd = new ExploreEntityEventArgs(this._Entity, EntityType.Grants);
            if (this.ExploreSelectedEntity != null)
                this.ExploreSelectedEntity(argd);
        }

        #region Overrides of EntityControlBase

        public override void SwitchableControl(BitmapImage imgSrc, bool selected)
        {
            if (_Visual.Shape == VerticeShape.Custom)
            {
                if (this.img != null && imgSrc != null)
                {
                    Size size = new Size(this.img.Width, this.img.Height);
                    this.img.Source = imgSrc;
                    this.img.SetValue(Image.WidthProperty, size.Width);
                    this.img.SetValue(Image.HeightProperty, size.Height);
                }
            }
        }

        public override void SetDisabled(bool disabled)
        {
            if (this.img != null)
            {
                if (disabled)
                    this.img.SetValue(Image.OpacityProperty, 0.1);
                else
                    this.img.SetValue(Image.OpacityProperty, 1.0);
            }
        }

        public override void SetHighlighted(bool highlighted)
        {
            if (highlighted)
                this.State = VerticeState.Highlighted;
            else
                this.State = VerticeState.Normal;

            if (_Visual.Shape == VerticeShape.Custom)
            {
                if (highlighted)
                {
                    this.border1.BorderBrush = new SolidColorBrush(Colors.Red);
                    this.border1.BorderThickness = new Thickness(1.0);
                }
                else
                {
                    this.border1.BorderThickness = new Thickness(0);
                }
            }
            else
            {
                if (highlighted)
                {
                    this.border2.BorderBrush = new SolidColorBrush(Colors.Red);
                    this.border2.BorderThickness = new Thickness(1.0);
                }
                else
                {
                    this.border2.BorderThickness = new Thickness(0.0);
                }
            }

            if (this.ShowLabel(_Entity, _Visual, _Measure))
            {
                this.label.Visibility = Visibility.Visible;
                this.label.Foreground = new SolidColorBrush(_Visual.LabelColor);
            }
            else
                this.label.Visibility = Visibility.Collapsed;

            this.InvalidateVisual();
        }

        public override Size ImageSize
        {
            get
            {
                if (this._Visual.Shape == VerticeShape.Custom)
                    return new Size(this.img.Width, this.img.Height);
                else
                    return NodeHelper.GetScaledSize(_Visual, _Measure, _Range);
            }
        }

        public override void UpdateVisual()
        {
            if (_Visual.Shape == VerticeShape.Custom)
            {
                this.img.Visibility = Visibility.Visible;
                this.img.Source = NodeHelper.CreateImage(_Visual, _Measure, _Range, _Selected);
                Size size = NodeHelper.GetScaledSize(_Visual, _Measure, _Range);
                this.img.SetValue(Image.WidthProperty, size.Width);
                this.img.SetValue(Image.HeightProperty, size.Height);
            }
            else
            {
                this.img.Visibility = Visibility.Collapsed;
                this.Container.Children.Add(NodeHelper.CreateShape(_Visual, _Measure, this._Entity.Flag, _Range, _Selected));
            }
        }

        #endregion

        private void cmu_DefineNewRule_Click(object sender, RoutedEventArgs e)
        {
            NewRuleFromNetworkEntityArgs args = new NewRuleFromNetworkEntityArgs(this._Entity);
            if (this.DefineNewRelation != null)
                this.DefineNewRelation(args);
        }
    }
}
